<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <div class="flex AIC">
          <h1 class="f16">参数定义</h1>
          <JDialog
            :tableHeader="JTableHeader"
            :width="49"
            @removeOne="removeOne"
            :currentTableRow="currentTableRow"
            @confirm="confirm"
            :unEdit="true"
          />
        </div>
        <Table :tableHeader="tableHeader" :tableData="tableData" class="mt10" :isLoading="isLoading" @currentRow="currentRow" />
        <PageModule
          :PageData="{ total, pageSizeShow: true, buttonS: true }"
          :layout="'prev, pager'"
          @pageSizeChange="handleSizeChange"
          @pageChange="handleCurrentChange"
        />
      </el-card>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import { formateData } from '@/plugins/mixinJ'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'ParameterDefines'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'

const formList = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '类别',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '请输入名称',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'button',
    checkboxLabel: '重置'
  }
]) as any
// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
const tableHeader = ref([
  {
    label: '类别',
    prop: 'type'
  },
  {
    label: '编码',
    prop: 'code'
  },
  {
    label: '名称',
    prop: 'name'
  },
  {
    label: '创建者',
    prop: 'sysUser'
  },
  {
    label: '创建时间',
    prop: 'sysDate'
  }
])
const JTableHeader = ref([
  {
    type: 'selectJ',
    label: '类别',
    prop: 'type',
    options: []
  },
  {
    label: '编码',
    prop: 'code'
  },
  {
    label: '名称',
    prop: 'name'
  }
]) as any
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.get<any, any>(
    `/sdStyle/getParamSet?type=${
      formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '类别')?.selectValue
    }&name=${
      formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入名称'
      )?.inputValue
    }&pageNum=${currentPage.value}&pageSize=${pageSize.value}`
  )
  isLoading.value = false
  tableData.value = tableDataList.list
  total.value = tableDataList.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
}
const confirm = async (obj: any, dialogVisible: any, title: any) => {
  // 向服务器发送请求，更新外派单信息
  if (title === '添加') {
    const res = (await LoginWork.post(`/sdStyle/optionParam?choose=${1}`, {
      ...obj,
      type: obj.type.value
    })) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success(res.message)
    } else {
      JMessage.error(res.message)
    }
  }
  // 关闭弹出框
  dialogVisible.value = false
  // 重新获取表格数据
  getTableList()
}
// 删除
const removeOne = async () => {
  // 向服务器发送请求，更新外派单信息
  const res = (await LoginWork.post(`/sdStyle/optionParam?choose=${2}`, currentTableRow.value)) as any
  // 判断请求是否成功
  if (res.code === 200) {
    JMessage.success(res.message)
  } else {
    JMessage.error(res.message)
  }
  // 重新获取表格数据
  getTableList()
}

const getSelectList = async () => {
  formList.value
    .find((item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '类别')
    .selectOptions.push(
      {
        optionValue: '工艺',
        optionLabel: '工艺'
      },
      {
        optionValue: '阶段',
        optionLabel: '阶段'
      },
      {
        optionValue: '问题类型',
        optionLabel: '问题类型'
      },
      {
        optionValue: '订单类型',
        optionLabel: '订单类型'
      }
    )
  JTableHeader.value[0].options?.push(
    {
      value: '工艺',
      label: '工艺'
    },
    {
      value: '阶段',
      label: '阶段'
    },
    {
      value: '问题类型',
      label: '问题类型'
    },
    {
      value: '订单类型',
      label: '订单类型'
    }
  )
}
onMounted(() => {
  getSelectList()
})
</script>
<style lang="less" scoped>
table {
  table-layout: auto !important;
}

:deep(.el-table__header) {
  width: 100% !important;
}

:deep(.el-table__body) {
  width: 100% !important;
}
</style>
